﻿using Common.DataModels;
using Extensions.Attribute.TransactionAttribute;
using Microsoft.AspNetCore.Mvc;
using PLC.Domain.Dto;
using PLC.Domain.Service.Service;

namespace PLC.API.Controllers
{
    [Route("api/data-config")]
    [ApiController]
    public class DataConfigController : BaseApiController
    {
        private readonly DataConfigService _dataConfigService;

        public DataConfigController(DataConfigService dataConfigService)
        {
            _dataConfigService = dataConfigService?? throw new ArgumentNullException(nameof(dataConfigService));
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pagingModel">分页阐述</param>
        /// <param name="configType">配置类型</param>
        /// <param name="deviceConfigId">设备配置的id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<DtoDataConfig>>> FindByPage([FromQuery]PagingModel pagingModel,int configType,long deviceConfigId)
        {
            var result = await _dataConfigService.FindByPageAsync(pagingModel,configType,deviceConfigId);
            return Success(result.Item1, result.Item2);
        }
        /// <summary>
        /// 精确查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<MessageModel<DtoDataConfig>> FindOne([FromRoute] long id)
        {
            var data = await _dataConfigService.FindOneAsync(id);
            return Success(data, "查询成功");
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="dtoDataConfig"></param>
        /// <returns></returns>
        [HttpPost]
        [Transaction]
        public async Task<MessageModel<DtoDataConfig>> Create([FromBody]DtoDataConfig dtoDataConfig)
        {
            // 需要返回创建的对象
            var data =  await _dataConfigService.CreateAsync(dtoDataConfig);
            return Success(data,"创建成功");
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dtoDataConfig"></param>
        /// <returns></returns>
        [HttpPut]
        [Transaction]
        public async Task<MessageModel<DtoDataConfig>> Update([FromBody] DtoDataConfig dtoDataConfig)
        {
            var data = await _dataConfigService.UpdateAsync(dtoDataConfig);
            return Success(data, "修改成功");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        [Transaction]
        public async Task<MessageModel> Delete([FromBody] List<long> ids)
        {
            await _dataConfigService.DeleteAsync(ids);
            return Success("删除成功");
        }
    }
}
